﻿using System;
using System.Globalization;

namespace Common.Helpers
{
    public class DateUtil
    {

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormatDate(DateTime date, string format = "yyyy-MM-dd")
        {
            return date.ToString(format);
        }


        /// <summary>
        /// 解析成日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime ParseDate(string date)
        {
            if (DateTime.TryParse(date, out DateTime dateTime))
            {
                return dateTime;
            }

            return DateTime.MinValue;
        }

        /// <summary>
        /// 解析成日期
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ParseDate(long timestamp)
        {
            // 转换为 DateTimeOffset  
            DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);

            // 如果需要本地时间，可以使用 ToLocalTime()  
            DateTime localDateTime = dateTimeOffset.LocalDateTime;

            return localDateTime;
        }


        public static long CrrentTimeMilliseconds()
        {
            return TimeMilliseconds(DateTime.Now);
        }


        public static long TimeMilliseconds(DateTime dateTime)
        {
            // 将 DateTime 转换为 DateTimeOffset  
            DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime);

            // 获取毫秒时间戳  
            long millisecondsTimestamp = dateTimeOffset.ToUnixTimeMilliseconds();


            return millisecondsTimestamp;
        }



        /// <summary>
        /// 判断时间是否是今天
        /// </summary>
        /// <returns></returns>
        public static bool IsToday(DateTime date)
        {
            var today = DateTime.Now;
            return FormatDate(today, "yyyy-MM-dd") == FormatDate(date, "yyyy-MM-dd");
        }


        /// <summary>
        /// 判断两个日期是否是同一年
        /// </summary>
        /// <param name="date"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static bool IsSameYear(DateTime date, DateTime date2)
        {
            return date.Year == date2.Year;
        }

        /// <summary>
        /// 判断两个日期是否是同一月
        /// </summary>
        /// <param name="date"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static bool IsSameMonth(DateTime date, DateTime date2)
        {
            return IsSameYear(date, date2) && date.Month == date2.Month;
        }

        /// <summary>
        /// 判断是否是同一天
        /// </summary>
        /// <param name="date"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static bool IsSameDay(DateTime date, DateTime date2)
        {
            return IsSameMonth(date, date2) && date.Day == date2.Day;
        }

        /// <summary>
        /// 判断是否同一周
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static bool IsSameWeek(DateTime date1, DateTime date2)
        {
            // 获取当前文化的日历
            CultureInfo cultureInfo = CultureInfo.CurrentCulture;
            Calendar calendar = cultureInfo.Calendar;

            // 获取日期对应的周数
            int weekOfYear1 = calendar.GetWeekOfYear(date1, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
            int weekOfYear2 = calendar.GetWeekOfYear(date2, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);

            // 判断年份是否相同
            return weekOfYear1 == weekOfYear2;
        }


        /// <summary>
        /// 获取当前日期是星期几
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetChineseDayOfWeek(DateTime date)
        {
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return "星期日";
                case DayOfWeek.Monday:
                    return "星期一";
                case DayOfWeek.Tuesday:
                    return "星期二";
                case DayOfWeek.Wednesday:
                    return "星期三";
                case DayOfWeek.Thursday:
                    return "星期四";
                case DayOfWeek.Friday:
                    return "星期五";
                case DayOfWeek.Saturday:
                    return "星期六";
                default:
                    return string.Empty;
            }
        }

    }
}
